using UnityEngine;
using XNode;
using System;

namespace JufGame
{
	public static class ExMethod
	{
		public static Vector4 Abs(this Vector4 vector)
		{
			Vector4 absVector = vector;
			if (vector.x < 0) absVector.x *= -1;
			if (vector.y < 0) absVector.y *= -1;
			if (vector.z < 0) absVector.z *= -1;
			if (vector.w < 0) absVector.w *= -1;
			return absVector;
		}
		public static bool AllGreater(this Vector4 vector, float value)
        {
			return vector.x > value && vector.y > value && vector.z > value && vector.w > value;
        }
		public static bool AllGreaterEquel(this Vector4 vector, float value)
        {
			return vector.x >= value && vector.y >= value && vector.z >= value && vector.w >= value;
        }
		public static bool AnyLess(this Vector4 vector, float value)
		{
			return vector.x < value || vector.y < value || vector.z < value || vector.w < value;
		}
		public static bool AnyLessEqual(this Vector4 vector, float value)
		{
			return vector.x <= value || vector.y <= value || vector.z <= value || vector.w <= value;
		}
		public static bool AnyGreater(this Vector4 vector, float value)
		{
			return vector.x > value || vector.y > value || vector.z > value || vector.w > value;
		}
		public static bool AnyGreaterEqual(this Vector4 vector, float value)
		{
			return vector.x >= value || vector.y >= value || vector.z >= value || vector.w >= value;
		}
		public static bool AllLessThan(this Vector4 vector, float value)
		{
			return vector.x < value && vector.y < value && vector.z < value && vector.w < value;
		}
		public static bool AllLessEquel(this Vector4 vector, float value)
		{
			return vector.x <= value && vector.y <= value && vector.z <= value && vector.w <= value;
		}
		public static Vector3 Abs(this Vector3 vector)
		{
			Vector3 absVector = vector;
			if (vector.x < 0) absVector.x *= -1;
			if (vector.y < 0) absVector.y *= -1;
			if (vector.z < 0) absVector.z *= -1;
			return absVector;
		}
		public static bool AllGreaterEquel(this Vector3 vector, float value)
		{
			return vector.x >= value && vector.y >= value && vector.z >= value;
		}
		public static bool AllLessThan(this Vector3 vector, float value)
		{
			return vector.x < value && vector.y < value && vector.z < value;
		}
		public static bool AnyLess(this Vector3 vector, float value)
		{
			return vector.x < value || vector.y < value || vector.z < value;
		}
		// ReSharper disable once SuggestBaseTypeForParameter, MemberCanBePrivate.Global
		public static int GetDynamicListPortIndex(this Node caller, NodePort port, string prefix) {
			if (port.node != caller) return -1; // you may or may not want this depending on what you want with this
			var success = int.TryParse(port.fieldName.Split(' ')[1], out var result);
			return port.fieldName.StartsWith(prefix) && success ? result : -1;
		}

		public static object WithDynamicListPort(this Node caller, NodePort port, string prefix, Func<int, object> function) {
			var index = caller.GetDynamicListPortIndex(port, prefix);
			return index == -1 ? null : function(index);
		}

		public static T WithDynamicListPort<T>(this Node caller, NodePort port, string prefix, Func<int, T> function) {
			var index = caller.GetDynamicListPortIndex(port, prefix);
			return index == -1 ? default : function(index);
		}
	}
}
